<?php

//商品风格标签
namespace App\Http\Controllers\Admin\Mall;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProductStyleController extends BaseController
{
    private $model;

    public function __construct()
    {
        $this->model = app('model_products_style');
    }

    /**
     * @api {get} /mall/product-style/all 获取所有列表
     *
     * @apiGroup 个性商城-风格标签
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": [
    {
    "id": 1,
    "name": "清新"
    },
    {
    "id": 2,
    "name": "可爱"
    }
    ]
    }
     */
    public function all()
    {
        $list = $this->model->getAll();
        return Response::sendData($list);
    }

    /**
     * @api {get} /mall/product-style/allShow 获取所有列表（下拉框）
     *
     * @apiGroup 个性商城-风格标签
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": [
    {
    "id": 1,
    "name": "清新"
    },
    {
    "id": 2,
    "name": "可爱"
    }
    ]
    }
     */
    public function allShow()
    {
        $list = $this->model->getAll(['is_show' => 1]);
        return Response::sendData($list);
    }

    /**
     * @api {post} /mall/product-style/add 添加风格标签
     *
     * @apiGroup 个性商城-风格标签
     *
     * @apiParam {String} name 名称，必须
     * @apiParam {Int} is_show 是否显示：1：是；0：否，必须
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": []
    }
     */
    public function add(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'name' => 'required|string',
                'is_show' => 'required|integer|in:1,0',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $name = $request->name;
        $isShow = $request->is_show;

        $style = $this->model->where('name', $name)->first();
        if ($style) {
            throw new BasicException(0, '名称已经存在');
        }

        $res = $this->model->insert([
            'name' => $name,
            'is_show' => $isShow,
        ]);
        if ($res) {
            return Response::sendData([]);
        }
    }

    /**
     * @api {put} /mall/product-style/add 编辑风格标签
     *
     * @apiGroup 个性商城-风格标签
     *
     * @apiParam {Int} id 风格id，必须
     * @apiParam {String} name 名称，必须
     * @apiParam {Int} is_show 是否显示：1：是；0：否
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": []
    }
     */
    public function edit(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|integer|min:1',
                'name' => 'required|string',
                'is_show' => 'required|integer|in:1,0',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $id = $request->id;
        $name = $request->name;
        $isShow = $request->is_show;

        $style = $this->model->where('name', $name)->where('id', '<>', $id)->first();
        if ($style) {
            throw new BasicException(0, '名称已经存在');
        }

        $res = $this->model->where('id', $id)->update([
            'name' => $name,
            'is_show' => $isShow,
        ]);
        if ($res) {
            return Response::sendData([]);
        } else {
            throw new BasicException(0, '编辑失败');
        }
    }

    /**
     * @api {delete} /mall/product-style/del 删除风格标签
     *
     * @apiGroup 个性商城-风格标签
     *
     * @apiParam {String} ids 风格id，多个用英文逗号连接，必须
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": []
    }
     */
    public function del(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'ids' => 'required|string',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $ids = explode(',', $request->ids);

        DB::beginTransaction();
        try {
            $flag = 0;
            foreach ($ids as $id) {
                if ($id) {
                    $flag = 1;
                    $style = $this->model->select('id', 'name')->where('id', $id)->first();
                    if (empty($style)) throw new BasicException(0, '风格id错误');

                    //是否有商品
                    $productExist = app('model_product_style_join')->select('id')->where('style_id', $id)->first();
                    if ($productExist) throw new BasicException(0, "标签‘{$style->name}’还有关联商品，无法删除");
                    $style->delete();
                }
            }
            if ($flag == 0) throw new BasicException(0, '请选择要删除的风格标签');

            DB::commit();
            return Response::sendData([]);
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(0, $e->getMessage());
        }
    }
}